package com.xunyin.modules.store.rest;

import com.xunyin.api.ApiResult;
import com.xunyin.api.XunyinException;
import com.xunyin.enums.ErrorEnum;
import com.xunyin.modules.log.AppLog;
import com.xunyin.modules.mainpart.service.TMainPartService;
import com.xunyin.modules.mainpart.vo.TMainPartOnlyNameVo;
import com.xunyin.modules.store.domain.TStore;
import com.xunyin.modules.store.param.StoreQueryParam;
import com.xunyin.modules.store.param.TStoreQueryParam;
import com.xunyin.modules.store.service.TStoreService;
import com.xunyin.modules.store.vo.TStoreVo;
import com.xunyin.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 主体门店控制器
 * </p>
 *
 * @author admin
 * @since 2022-9-29
 */
@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(value = "主体门店模块", tags = "共享空间:主体门店模块")
public class StoreController {

    private final TMainPartService tMainPartService;

    private final TStoreService tStoreService;

    /**
     * 根据小程序Id-获取主体id
     * 再根据主体id-获取门店列表
     */
    @AppLog(value = "获取门店列表", type = 1)
    //@AuthCheck
    @PostMapping("/getShopSelectData")
    @ApiOperation(value = "门店列表", notes = "门店列表")
    public ApiResult<List<TStoreVo>> getShopSelectData(@Validated @RequestBody StoreQueryParam queryParam) {
        log.info("getShopSelectData -> appId={}", queryParam.getAppId());
        TMainPartOnlyNameVo tMainPartOnlyNameVo = tMainPartService.getMainPartByAppId(queryParam.getAppId());
        if (tMainPartOnlyNameVo == null) {
            //return ApiResult.fail(ErrorEnum.E_90006.getErrorMsg())
            throw new XunyinException(ErrorEnum.E_90006.getErrorMsg());
        }
        TStoreQueryParam storeQueryParam = new TStoreQueryParam();
        storeQueryParam.setMainId(tMainPartOnlyNameVo.getId());
        List<TStoreVo> list = tStoreService.listStore(storeQueryParam);
        if (list == null) {
            throw new XunyinException("无数据");
        }
        return ApiResult.ok(list);
    }


    /**
     * 根据管理员手机号码获取门店信息
     */
    @AppLog(value = "根据管理员手机号码获取门店信息", type = 1)
    //@AuthCheck
    @PostMapping("/manage/getInfoTeaByManagePhone")
    @ApiOperation(value = "根据管理员手机号码获取门店信息", notes = "根据管理员手机号码获取门店信息")
    public ApiResult<TStore> getInfoTeaByManagePhone(@Validated @RequestBody TStoreQueryParam queryParam) throws IOException {
        if (StringUtils.isEmpty(queryParam.getManagePhone())) {
            throw new XunyinException(ErrorEnum.E_90004.getErrorMsg());
        }
        //String managePhone = queryParam.getManagePhone();
        TStore tStore = tStoreService.getInfoTeaByManagePhone(queryParam);
        if (tStore == null) {
            throw new XunyinException(ErrorEnum.E_90004.getErrorMsg());
        } else {
            return ApiResult.ok(tStore);
        }
    }

}
